package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        Log.v("tag_main", "onCreate LoginActivity");
        Button login = (Button) findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {


            public void onClick(View v) {

                // Toast.makeText(LoginActivity.this, "按下登陆按钮", Toast.LENGTH_SHORT).show();

                EditText usr = (EditText) findViewById(R.id.usr);
                EditText psw = (EditText) findViewById(R.id.psw);
                login(usr.getText().toString().trim(), psw.getText().toString().trim());

            }
        });
        Button go_reg = (Button) findViewById(R.id.go_register);
        //为button 按钮添加监听事件
        go_reg.setOnClickListener(new View.OnClickListener() {


            public void onClick(View v) {

                Toast.makeText(LoginActivity.this, "进入注册页面!!", Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });


    }


    void login(String usr, String psw) {
        if (usr.length() == 0 || psw.length() == 0) {

            Toast.makeText(LoginActivity.this, "请输入完整信息!", Toast.LENGTH_SHORT).show();
            return;
        } else if (usr.equals("test") && psw.equals("test")) {


            Toast.makeText(LoginActivity.this, "登陆成功!", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);

        }else {

            Toast.makeText(LoginActivity.this, "登陆信息错误!", Toast.LENGTH_SHORT).show();


        }

    }

    @Override
    public void onBackPressed() {

        super.onBackPressed();
    }

}
